package com.ruoyi.sycs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sycs.domain.bo.BaQjDetailBo;
import com.ruoyi.sycs.domain.vo.BaQjDetailVo;
import com.ruoyi.sycs.domain.BaQjDetail;
import com.ruoyi.sycs.mapper.BaQjDetailMapper;
import com.ruoyi.sycs.service.IBaQjDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 器具明细表Service业务层处理
 *
 * @author zyb
 * @date 2024-04-12
 */
@RequiredArgsConstructor
@Service
public class BaQjDetailServiceImpl implements IBaQjDetailService {

    private final BaQjDetailMapper baseMapper;

    /**
     * 查询器具明细表
     */
    @Override
    public BaQjDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询器具明细表列表
     */
    @Override
    public TableDataInfo<BaQjDetailVo> queryPageList(BaQjDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaQjDetail> lqw = buildQueryWrapper(bo);
        Page<BaQjDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询器具明细表列表
     */
    @Override
    public List<BaQjDetailVo> queryList(BaQjDetailBo bo) {
        LambdaQueryWrapper<BaQjDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaQjDetail> buildQueryWrapper(BaQjDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaQjDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getQjId() != null, BaQjDetail::getQjId, bo.getQjId());
//        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), BaQjDetail::getRemark, bo.getRemark());
        lqw.like(StringUtils.isNotBlank(bo.getHh()), BaQjDetail::getHh, bo.getHh());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getQuality()), BaQjDetail::getQuality, bo.getQuality());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getTongrj()), BaQjDetail::getTongrj, bo.getTongrj());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getHTime()), BaQjDetail::getHTime, bo.getHTime());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getHPer()), BaQjDetail::getHPer, bo.getHPer());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getDeptId()), BaQjDetail::getDeptId, bo.getDeptId());
        return lqw;
    }

    /**
     * 新增器具明细表
     */
    @Override
    public Boolean insertByBo(BaQjDetailBo bo) {
        BaQjDetail add = BeanUtil.toBean(bo, BaQjDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改器具明细表
     */
    @Override
    public Boolean updateByBo(BaQjDetailBo bo) {
        BaQjDetail update = BeanUtil.toBean(bo, BaQjDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaQjDetail entity){
        //TODO 做一些数据校验,如唯一约束
        if (baseMapper.exists(new LambdaQueryWrapper<BaQjDetail>()
            .eq(ObjUtil.isNotNull(entity.getDeptId()), BaQjDetail::getDeptId, entity.getDeptId())
            .eq(ObjUtil.isNotNull(entity.getQjId()), BaQjDetail::getQjId, entity.getQjId())
            .eq(ObjUtil.isNotNull(entity.getHh()), BaQjDetail::getHh, entity.getHh())
            .ne(ObjUtil.isNotNull(entity.getDetailId()), BaQjDetail::getDetailId, entity.getDetailId()))) {
            throw new ServiceException("您的试验室该类型的盒（筒）中已经存在此盒号，请勿重复添加。");
        }
    }

    /**
     * 批量删除器具明细表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 器具明细表分页
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<BaQjDetailVo> page(BaQjDetailBo bo, PageQuery pageQuery) {
        if (ObjectUtils.isEmpty(bo.getDeptId())) {
            throw new ServiceException("请选择试验室");
        }
        IPage<BaQjDetailVo> page = baseMapper.page(bo, pageQuery.build());
        return TableDataInfo.build(page);
    }

    @Override
    public TableDataInfo<BaQjDetailVo> usablePage(BaQjDetailBo bo, PageQuery pageQuery) {
        IPage<BaQjDetailVo> page = baseMapper.usablePage(bo, pageQuery.build());
        return TableDataInfo.build(page);
    }

}
